package com.zhou.controller;

import com.alibaba.fastjson.JSONObject;
import com.zhou.dto.Result;
import com.zhou.entity.Student;
import com.zhou.service.StudentService;
import com.zhou.utils.PinyinUtils;
import com.zhou.utils.StringToListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @CrossOrigin
    @PostMapping("findStudentByMajorID")
    public Result findStudentByMajorID(@RequestBody String data){
        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        int major_id = Integer.valueOf(map.get("data").toString()).intValue();
        List<Student> students = studentService.findStudentByMajorID(major_id);
        return Result.SUCCESS.setNewData(students);
    }

    @CrossOrigin
    @PostMapping("addStudent")
    public Result addStudent(@RequestBody String data){

        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        // 取出map中的data,继续进行Json解析
        JSONObject jsonObject2 = JSONObject.parseObject(map.get("data").toString());
        Map<String, Object> map2 = new HashMap<>();
        map2.putAll(jsonObject2);
        // 取出map中的major,继续进行Json解析
        JSONObject jsonObject3 = JSONObject.parseObject(map2.get("major").toString());
        Map<String, Object> map3 = new HashMap<>();
        map3.putAll(jsonObject3);

        Student student = new Student();
        // 取出学生们的名字，进行分割处理
        String allStudents_name = map2.get("students_name").toString();
        int department_id = Integer.valueOf(map2.get("department_id").toString()).intValue();
        int major_id = Integer.valueOf(map3.get("id").toString()).intValue();
        int grade = Integer.valueOf(map2.get("grade").toString()).intValue();
        int class_id = Integer.valueOf(map2.get("class").toString()).intValue();
        int year = grade+2000;

        /**
         * 学号插入算法
         */
        long prefix=(grade*100000000+department_id*100000+major_id*100+class_id);
        // 通过学号前缀查询同一个前缀下哪个学号最大
        Long i = studentService.maxStudentId(prefix);
        // 查不到，就使用这个前缀，从001开始
        if(i ==null){
            i = 1+prefix*1000;
        }
        // 查到了，就从最大值开始加1，加完1后下一步再进行赋值操作
        else {
            i++;
        }
        long StudentId=i;

        // 使用字符串转集合工具类，把一大堆名字分割并组成集合类型
        List<String> students_name = StringToListUtils.result(allStudents_name);
        //创建拼音工具类对象（需要pinyin4j依赖），进行拼音排序
        PinyinUtils pinyinUtils = new PinyinUtils();
        Collections.sort(students_name,pinyinUtils);

        for (String student_name:students_name) {
            System.out.println(student_name);
            if (!student_name.equals("")){
                // 学号赋完值后进行加1操作
                student.setId(StudentId++);
                //  student.setSex(sex);
                student.setYear(year);
                student.setName(student_name);
                student.setMajor_id(major_id);
                // 把Student对象放进集合中
                List<Student> students = new ArrayList<>();
                students.add(student);
                System.out.println(students);
                studentService.addStudent(students);
            }
        }
        return Result.INSERT_SUCCESS;
    }

    @CrossOrigin
    @PostMapping("deleteStudent")
    public Result deleteStudent(@RequestBody String data){
        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);

        // 取出map中的data,继续进行Json解析
        JSONObject jsonObject2 = JSONObject.parseObject(map.get("data").toString());
        Map<String, Object> map2 = new HashMap<>();
        map2.putAll(jsonObject2);
        // 取出学生ID
        long student_id = Long.valueOf(map2.get("id").toString()).longValue();
        studentService.deleteStudent(student_id);
        return Result.DELETE_SUCCESS;
    }
}
